﻿using System ;
using System.Windows ;
using System.Windows.Data ;

namespace OpenLightGroup.Common.Converters
    {
        /// <summary>
        /// Used to convert Boolean values to Visible or Collapsed. 
        /// Note: the parameter can be set to the default Boolean value to use as a fallback.
        /// </summary>
        public class BooleanVisibilityConverter
        : IValueConverter
            {
                private InverseBooleanConverter _boolInverter = new InverseBooleanConverter( ) ;

                #region IValueConverter Members
                /// <summary>
                /// Converts an Boolean value to a Visibility value.
                /// </summary>
                /// <param name="value">Boolean value</param>
                /// <param name="targetType">The System.Type of data expected by the target dependency property.</param>
                /// <param name="parameter">If the parameter is the string "inverse" the conversion will be inverted.</param>
                /// <param name="culture">The culture of the conversion.</param>
                /// <returns>The value to be passed to the target dependency property.</returns>
                public object Convert ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
                    {
                        if ( parameter != null && parameter.ToString( ).ToLower( culture ) == "inverse" )
                            value = this._boolInverter.Convert( value, targetType, parameter, culture ) ;

                        return ConvertBooleanToVisibility( value ) ;
                    }

                /// <summary>
                /// Converts Visibility value to Boolean
                /// </summary>
                /// <param name="value">Visibility value</param>
                /// <param name="targetType">The System.Type of data expected by the source object.</param>
                /// <param name="parameter">If the parameter is the string "inverse" the conversion will be inverted.</param>
                /// <param name="culture">The culture of the conversion.</param>
                /// <returns>The value to be passed to the source object.</returns>
                public object ConvertBack ( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
                    {
                        bool? output = ConvertVisibilityToBoolean( value ) ;

                        if ( parameter != null && parameter.ToString( ).ToLower( culture ) == "inverse" )
                            output = !output ;

                        return output ;
                    }

                #endregion

                private static Visibility? ConvertBooleanToVisibility ( object value )
                    {
                        Visibility? output = null ;

                        if ( value != null && !string.IsNullOrEmpty( value.ToString( ) ) )
                        {
                            bool boolValue ;

                            if ( bool.TryParse( value.ToString( ), out boolValue ) )
                            {

                                if ( boolValue )
                                    output = Visibility.Visible ;
                                else
                                    output = Visibility.Collapsed ;

                            }
                        }

                        return output ;
                    }

                private static bool? ConvertVisibilityToBoolean ( object value )
                    {

                        bool? output = null ;

                        if ( value != null && !string.IsNullOrEmpty( value.ToString( ) ) )
                        {
                            Visibility? visValue = null ;

                            if ( Enum.IsDefined( typeof(Visibility), value ) )
                                visValue = (Visibility)Enum.Parse( typeof(Visibility), value.ToString( ), true ) ;

                            if ( visValue != null )
                            {

                                if ( visValue == Visibility.Collapsed )
                                    output = false ;
                                else
                                    output = true ;

                            }
                        }

                        return output ;

                    }

            }
    }